import React from 'react';
import { Modal, Form, Input } from 'antd';

const FormItem = Form.Item;

class AddModal extends React.Component {
  constructor (props) {
    super(props);
    this.state = {};
    this.onOK = this.onOK.bind(this);
  }

  onOK () {
    const { onOK, form, onCancel, record, getList } = this.props;
    form.validateFields((err, values) => {
      if (!err) {
        onOK({ ...values, id: record.id }).then(() => {
          onCancel();
          getList();
        });
      }
    });
  }

  render () {
    const { 
      onCancel, 
      form,
      loading, 
      record = {}
   } = this.props;
    const { getFieldDecorator } = form;
    const modalOpts = {
      confirmLoading: loading,
      title: `${record.id ? '编辑' : '新增'}模版`,
      width: 500,
      visible: true,
      maskClosable: false,
      okText: '确定',
      cancelText: '取消',
      onOk: this.onOK,
      onCancel
    };
    const formItemLayout = {
      labelCol: {
        span: 6
      },
      wrapperCol: {
        span: 18
      }
    };
    return (
      <Modal {...modalOpts}>
        <Form>
          <FormItem
            label='模版名称'
            {...formItemLayout}
          >
            {
              getFieldDecorator('name', {
                initialValue: record.name || '',
                rules: [{ required: true, message: '请填写模版名称' }]
              })(
                <Input
                  maxLength={30}
                  placeholder='模版名称'
                />
              )
            }
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(AddModal);
